package com.example.maggie.meizinote.mvp.presenter;

import android.app.Application;
import android.app.Dialog;
import android.graphics.Color;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.maggie.meizinote.app.MApp;
import com.example.maggie.meizinote.app.bean.note_item;
import com.example.maggie.meizinote.mvp.listener.AddUrlListener;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.example.maggie.meizinote.mvp.contract.NoteEditContract;

import java.io.File;
import java.util.ArrayList;


@ActivityScope
public class NoteEditPresenter extends BasePresenter<NoteEditContract.Model, NoteEditContract.View> implements AddUrlListener, BaseQuickAdapter.OnItemClickListener {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    private String classifyId;
    private String id;
    private boolean idDelect = false;
    private int mType;

    @Inject
    public NoteEditPresenter(NoteEditContract.Model model, NoteEditContract.View rootView) {
        super(model, rootView);
    }

    public void setClassifyId(String classifyId) {
        this.classifyId = classifyId;
    }

    public void setId(String id) {
        this.id = id;
        getNowNoteData();
    }

    public void delect() {
        note_item gameScore = new note_item();
        gameScore.setObjectId(id);
        gameScore.setUser_id(MApp.getUserId());
        gameScore.delete(new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.i("bmob", "成功");
                    idDelect = true;
                    mRootView.killMyself();
                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }

    public void updataNote(String title, String content) {
        if (idDelect) return;
        note_item gameScore = new note_item();
        gameScore.setClassify_id(classifyId);
        gameScore.setNote_title(title);
        gameScore.setContent(content);
        gameScore.setUser_id(MApp.getUserId());
        gameScore.update(id, new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.i("bmob", "更新成功");
                } else {
                    Log.i("bmob", "更新失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }

    public void updataImg(File file) {
        mRootView.showLoading();
        BmobFile bmobFile = new BmobFile(file);
        bmobFile.uploadblock(new UploadFileListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    //bmobFile.getFileUrl()--返回的上传文件的完整地址
                    mRootView.setImg(bmobFile.getFileUrl(), bmobFile.getFileUrl());
                } else {
                    mRootView.showMessage("文件上传失败");
                }
                mRootView.hideLoading();
            }

            @Override
            public void onProgress(Integer value) {
                // 返回的上传进度（百分比）
            }
        });
    }

    public void createNote() {
        note_item gameScore = new note_item();
        gameScore.setUser_id(MApp.getUserId());
        gameScore.save(new SaveListener<String>() {

            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    id = objectId;
                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }

    public void getNowNoteData() {
        BmobQuery<note_item> query = new BmobQuery<note_item>();
        query.getObject(id, new QueryListener<note_item>() {

            @Override
            public void done(note_item object, BmobException e) {
                if (e == null) {
                    mRootView.setNowData(object);
                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }

        });
    }

    /**
     * @param type 0 文字颜色  1 背景颜色
     */
    public void selectColor(int type) {
        mType = type;
        mRootView.showColorDialog(type == 0 ? "选择文字颜色" : type == 1 ? "选择背景颜色" : "", type);
    }

    public void setColor(int color) {
        if (mType == 0) mRootView.setTextColor(color);
        if (mType == 1) mRootView.setTextBackgroundColor(color);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    @Override
    public void addUrl(Dialog dialog, int a, String title, String url) {
        if (!(url.contains("http://") || url.contains("https://")))
            url = "http://" + url;
        mRootView.setUrl(title, url);
        dialog.dismiss();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        setColor((int) adapter.getItem(position));
    }
}
